package com.jucai.pppeiyouclient.adapter;

import android.content.Context;
import android.support.v4.content.ContextCompat;
import android.text.TextUtils;
import android.view.Gravity;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.LinearLayout;

import com.jucai.pppeiyouclient.R;
import com.jucai.pppeiyouclient.base.CommonRecyclerViewAdapter;
import com.jucai.pppeiyouclient.bean.SkillBean;
import com.jucai.pppeiyouclient.bean.SkillGroupBean;
import com.jucai.pppeiyouclient.utils.UnitUtil;
import com.jucai.pppeiyouclient.weidget.taglayout.TagLayout;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Kuky on 2017-05-04.
 * 修改
 * 2017-5-14 14:26:24
 */

public class SkillGroupAdapter extends CommonRecyclerViewAdapter<SkillGroupBean> {
    private static final String TAG = "SkillGroupAdapter : ";
    List<SkillBean> list = new ArrayList<SkillBean>();

    OnSelectedSkillListener onSelectedSkillListener;

    public void setOnSelectedSkillListener(OnSelectedSkillListener onSelectedSkillListener) {
        this.onSelectedSkillListener = onSelectedSkillListener;
    }

    public SkillGroupAdapter(Context context, List<SkillGroupBean> data) {
        super(context, data);
    }

    @Override
    public void bindHolder(CommonHolder holder, SkillGroupBean skillGroupBean) {
        holder.setText(R.id.skill_group_name, skillGroupBean.getSkill_cate_name());
        TagLayout skillTagLayout = holder.getView(R.id.skill_tag_layout);
        final List<SkillBean> skills = skillGroupBean.getSkill_list();

        for (int i = 0; i < skills.size(); i++) {
            final CheckBox view = new CheckBox(mContext);
            LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(UnitUtil.dipToPx(mContext, 72), UnitUtil.dipToPx(mContext, 28));
            view.setLayoutParams(layoutParams);
            view.setBackgroundResource(R.drawable.border_check_box_skill_normal);
            view.setText(skills.get(i).getSkill_name());
            view.setTextSize(12);
            view.setGravity(Gravity.CENTER);
            //view.setPadding(UnitUtil.dipToPx(mContext, 24), UnitUtil.dipToPx(mContext, 0), UnitUtil.dipToPx(mContext, 24), UnitUtil.dipToPx(mContext, 0));
            skillTagLayout.addView(view);
            view.setButtonDrawable(null);
            final int position = i;

            if (skills.get(i).getCert_state() == 0) {
                view.setBackgroundResource(R.drawable.border_check_box_skill_normal);
                view.setTextColor(ContextCompat.getColor(mContext, R.color.txt_grey));
                view.setChecked(false);
            } else {
                view.setBackgroundResource(R.drawable.border_check_box_skill_press);
                view.setTextColor(ContextCompat.getColor(mContext, R.color.black));
                view.setChecked(true);
            }

            view.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    if (!isChecked) {
                        view.setTextColor(ContextCompat.getColor(mContext, R.color.txt_grey));
                        view.setBackgroundResource(R.drawable.border_check_box_skill_normal);
                        list.remove(skills.get(position));
                    } else {
                        view.setTextColor(ContextCompat.getColor(mContext, R.color.black));
                        view.setBackgroundResource(R.drawable.border_check_box_skill_press);
                        list.add(skills.get(position));
                        if (onSelectedSkillListener != null)
                            onSelectedSkillListener.onClick(skills.get(position));
                    }
                }
            });
        }
    }

    private void remove(List<SkillBean> list, String getSkill_id) {
        List<SkillBean> lists = new ArrayList<SkillBean>();
        lists.addAll(list);
        for (SkillBean sb : lists) {
            if (TextUtils.equals(sb.getSkill_id(),getSkill_id)) {
                list.remove(sb);
            }
        }
    }

    @Override
    protected int getItemType(int position) {
        return 0;
    }

    @Override
    public int getLayoutId(int viewType) {
        return R.layout.item_skill_group_layout;
    }

    public List<SkillBean> getSelectList() {
        return list;
    }

    public interface OnSelectedSkillListener{
        void onClick(SkillBean skillBean);
    }

}
